﻿@{
    ViewBag.Title = "Roles";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model e.Store.Model.Entities.Role


<div class="col-lg-12">

    @using (Html.BeginForm("AddAndUpdateRole", "Administrator", FormMethod.Post, new { id = "roleForm" }))
    {
        <div id="productCrud" class="panel panel-danger">
            <div class="panel-heading">
                <h3 class="panel-title"><span id="title">Role</span> <span class="pull-right">

                    <a href="/Administrator/Roles" title="View All Roles" class="btn-sm btn-primary">
                        <span class="btn-label"><i class="icon-table"></i></span>View All Roles </a>
                </span></h3>
            </div>
            <div class="panel-body">

                <div class="col-lg-4 col-lg-offset-4">

                    @Html.DropDownList("lstSelectedValues", new SelectList(ViewBag.SelectedValues == null ? "" : ViewBag.SelectedValues))


                    @Html.HiddenFor(c => c.RoleOID, new { id = "txtRoleOID" })

                    <div class="form-group">
                        <label class="control-label" for="txtName">Role Name</label>

                        @Html.TextBoxFor(c => c.RoleName, new { id = "txtRoleName", @class = "form-control" })

                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtDescription">Selet Menu</label>
                        @Html.ListBoxFor(c => c.RefMenuRoles, ViewBag.Menulist as MultiSelectList, new { id = "sslMenu", @class = "form-control input-lg", @style = "height: 200px" })
                    </div>
                    <div class="form-group">
                        <label class="control-label" for="chkActive">Active</label>
                        @Html.CheckBoxFor(c => c.Active, new { id = "chkActive", @class = "form-control" })
                    </div>
                    <div class="form-group">
                        <div class="pull-right col-lg-offset-2">
                            <button id="btnSave" type="submit" class="btn btn-labeled btn-success">
                                <span class="btn-label"><i class="icon-check icon-white"></i></span>Save</button>

                            <button type="reset" class="btn btn-labeled btn-danger">
                                <span class="btn-label"><i class="icon-remove icon-white"></i></span>Cancel</button>

                        </div>
                    </div>

                </div>


            </div>
        </div>
    }



</div>

<script type="text/javascript">


    $(document).ready(function () {


        $("#lstSelectedValues").css("display", "none");

        if ($("#txtCategoryOID").val() == "") {
            $("#title").text('Add Role');
        }
        else {
            $("#title").text('Edit Role');

            $("#lstSelectedValues option").each(function () {
                $("#sslMenu option[value=" + $(this).text() + "]").attr('selected', true);
            });
        }

        //-----------------------------------------------
        var errors = "";

        $("#btnSave").click(function () {

            errors = "";

            if ($("#txtRoleName").val() == "") {
                errors = "Please enter role name.";
            }

            if ($("#txtDescription").val() == "") {
                errors = errors + "<br/> Please enter category description.";
            }

            if (errors != "") {
                create("themeroller", { title: "Error!", text: errors }, {
                    custom: true,
                    expires: false
                });
                event.preventDefault();
            }

            if ($("#txtRoleOID").val() == "") {
                return checkExistingRole();
            }
            return true;

        });

        //---------------------------------------------------------------
        function checkExistingRole() {

            var value = 0;
            $.ajax({
                type: "POST",
                async: false,
                url: '/Administrator/CheckRole',
                data: { roleName: $("#txtRoleName").val() },
                dataType: "json",
                processdata: true,
                success: function (result) {
                    var resultMsg = result.Message;
                    if (result.Success == true) {
                        create("themeroller", { title: 'Error!', text: result.Message }, {
                            custom: true,
                            expires: false
                        });
                        value = 1;
                    }
                },
                error: function (e) {

                }
            });

            if (value == 1)
                event.preventDefault();
        }
        //------------------------------------------------
    });// ready
</script>
